#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    t1 = new text1();

    ui->x1->setText("2005");
    ui->x2->setText("2010");
    ui->x3->setText("2015");
    ui->x4->setText("2020");
    ui->y1->setText("72.8");
    ui->y2->setText("74.2");
    ui->y3->setText("75.2");
    ui->y4->setText("76.4");
    ui->input->setText("2025");
    ui->textBrowser->append(tr("当前为:地区一"));
}

MainWindow::~MainWindow()
{
    delete ui;
}




void MainWindow::on_pushButton_3_clicked()
{
    if(region==1)
    {
        ui->y1->setText("70.2");
        ui->y2->setText("70.2");
        ui->y3->setText("70.3");
        ui->y4->setText("71.2");
        region = 2;
        ui->textBrowser->append(tr("当前为:地区二"));
    }
    else
    {
        ui->y1->setText("72.8");
        ui->y2->setText("74.2");
        ui->y3->setText("75.2");
        ui->y4->setText("76.4");
        region = 1;
        ui->textBrowser->append(tr("当前为:地区一"));
    }
}


void MainWindow::on_pushButton_4_clicked()
{
    t1->clear();
    t1->addY(ui->y1->text().toDouble());
    t1->addY(ui->y2->text().toDouble());
    t1->addY(ui->y3->text().toDouble());
    t1->addY(ui->y4->text().toDouble());

    t1->addX(ui->x1->text().toDouble());
    t1->addX(ui->x2->text().toDouble());
    t1->addX(ui->x3->text().toDouble());
    t1->addX(ui->x4->text().toDouble());

    ui->textBrowser->append(tr("-拉格朗日3次插值-"));
    ui->textBrowser->append(tr("x设置为-")+"  x1:"+ui->x1->text()+"  x2:"+ui->x2->text()+"  x3:"+ui->x3->text()+"  x4:"+ui->x4->text());
    ui->textBrowser->append(tr("y设置为-")+"  y1:"+ui->y1->text()+"   y2:"+ui->y2->text()+"   y3:"+ui->y3->text()+"   y4:"+ui->y4->text());
}


void MainWindow::on_pushButton_5_clicked()
{
    t1->apply(3,ui->input->text().toDouble());
    ui->textBrowser->append("预测点设置为"+ui->input->text());
    ui->textBrowser->append("-拉格朗日基函数-");
    for(int i=0;i<=3;i++)
    {
        ui->textBrowser->append("L"+QString::number(i)+"(x)="+QString::number(t1->getLagrange_base(i)));
    }
    ui->textBrowser->append("-根据地区"+QString::number(region)+"数据的预测结果-");
    ui->textBrowser->append("在"+ui->input->text()+"处使用拉格朗日3次插值预测得到"+QString::number(t1->getResult()));
    ui->textBrowser->append("----------------done----------------");
}



